package com.eedi.framework.permission.controller.org;

import com.eedi.framework.permission.controller.org.vo.permission.OrgPermissionAssignRoleDataScopeReq;
import com.eedi.framework.permission.controller.org.vo.permission.OrgPermissionAssignRoleMenuReq;
import com.eedi.framework.permission.controller.org.vo.permission.OrgPermissionAssignUserRoleReq;
import com.eedi.framework.permission.controller.org.vo.permission.OrgPermissionBatchAssignUserRoleResp;
import com.eedi.framework.common.pojo.CommonResult;
import com.eedi.framework.permission.controller.org.vo.permission.*;
import com.eedi.framework.permission.service.OrgPermissionService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Set;

import static com.eedi.framework.common.pojo.CommonResult.success;

/**
 * 权限 Controller，提供赋予用户、角色的权限的 API 接口
 *
 * @author 永聚长青源码
 */
@Tag(name = "经营界面 - 权限管理")
@RestController
@RequestMapping("/org-permission")
public class OrgPermissionController {

    @Resource
    private OrgPermissionService orgPermissionService;


    @Operation(summary = "获得角色拥有的菜单编号(org:permission:list)")
    @Parameter(name = "orgRoleId", description = "角色编号", required = true)
    @GetMapping("/list-role-menus")
    @PreAuthorize("@org.hasPermission('org:permission:list')")
    public CommonResult<Set<String>> getRoleMenuList(String orgRoleId) {
        return success(orgPermissionService.getRoleMenuListByRoleId(orgRoleId));
    }

    @PostMapping("/assign-role-menu")
    @Operation(summary = "赋予角色菜单(org:permission:update)")
    @PreAuthorize("@org.hasPermission('org:permission:update')")
    public CommonResult<Boolean> assignRoleMenu(@Validated @RequestBody OrgPermissionAssignRoleMenuReq reqVO) {
        // 开启多租户的情况下，需要过滤掉未开通的菜单
//        tenantService.handleTenantMenu(menuIds -> reqVO.getMenuIds().removeIf(menuId -> !CollUtil.contains(menuIds, menuId)));
        // 执行菜单的分配
        orgPermissionService.assignRoleMenu(reqVO.getOrgRoleId(), reqVO.getOrgMenuIds());
        return success(true);
    }

    @PostMapping("/assign-role-data-scope")
    @Operation(summary = "赋予角色数据权限(org:permission:update)")
    @PreAuthorize("@org.hasPermission('org:permission:update')")
    public CommonResult<Boolean> assignRoleDataScope(@Valid @RequestBody OrgPermissionAssignRoleDataScopeReq reqVO) {
        orgPermissionService.assignRoleDataScope(reqVO.getOrgRoleId(), reqVO.getOrgRoleDataScope(), reqVO.getOrgRoleDataScopeDeptIds());
        return success(true);
    }

    @Operation(summary = "获得管理员拥有的角色编号列表(org:permission:list)")
    @Parameter(name = "orgUserId", description = "用户编号", required = true)
    @GetMapping("/list-user-roles")
    @PreAuthorize("@org.hasPermission('org:permission:list')")
    public CommonResult<Set<String>> listAdminRoles(@RequestParam("orgUserId") String orgUserId) {
        return success(orgPermissionService.getUserRoleIdListByUserId(orgUserId));
    }

    @Operation(summary = "赋予用户角色(org:permission:update)")
    @PostMapping("/assign-user-role")
    @PreAuthorize("@org.hasPermission('org:permission:update')")
    public CommonResult<Boolean> assignUserRole(@Validated @RequestBody OrgPermissionAssignUserRoleReq reqVO) {
        orgPermissionService.assignUserRole(reqVO.getOrgUserId(), reqVO.getOrgRoleIds(),null);
        return success(true);
    }


    /**
     * 下载批量导入用户角色配置的excel模板
     * @param response
     * @throws IOException
     */
    @GetMapping(value = "/excel-template")
    @Operation(summary = "下载批量导入用户角色配置的excel模板")
    public void excelTemplate(HttpServletResponse response) throws IOException {
        orgPermissionService.excelTemplate(response);
    }

    /**
     * @author: TomBombadil
     * @desc: 下载fileUrl对应的excel文件，解析文件数据后入库保存
     * @param fileUrl
     * @return
     * @throws Exception
     */
    @PostMapping("/batch-assign-user-role")
    @Operation(summary = "批量为成员添加角色权限(org:permission:update)")
    @PreAuthorize("@org.hasPermission('org:permission:update')")
    public CommonResult<OrgPermissionBatchAssignUserRoleResp> batchAssignUserRole(@RequestParam("fileUrl") String fileUrl) throws Exception{
        return success(orgPermissionService.batchAssignUserRole(fileUrl));
    }
}
